package com.szu.subject.ui.mq;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;

import com.szu.subject.domains.Course;
import com.szu.subject.domains.CourseInfo;
import com.szu.subject.domains.StudentNum;
import com.szu.subject.domains.Selection;
import com.szu.subject.ui.mapper.SelectionMapper;
import com.szu.subject.ui.service.CourseService;

class IdComparator implements Comparator<Long>{

	@Override
	public int compare(Long o1, Long o2) {
		return (int) (o1-o2);
	}

	
}
public class ReceiveService {
	@Autowired
	private SelectionMapper selectionMapper;

	@Autowired
	private CourseService courseService;
	
	@JmsListener(destination = "selection.queue.first")
	void firstSelection(Message message) throws JMSException {
		ObjectMessage objMessage = (ObjectMessage) message;
		Selection selection = (Selection) objMessage.getObject();
		List<CourseInfo> courseInfos;
		List<CourseInfo> newCourseInfos;
		Double credit=selection.getCredit();
		if (selection.getOperationType()) {	//选课
			courseInfos=selection.getCourseInfos();
			newCourseInfos=new ArrayList<>(courseInfos.size());
			for(CourseInfo courseInfo:courseInfos){
				if(credit>courseInfo.getCredit()){
					newCourseInfos.add(courseInfo);
					credit-=courseInfo.getCredit();
				}
			}
			selection.setCourseInfos(newCourseInfos);
			selectionMapper.addCourses(selection);
		} else
			selectionMapper.removeCourses(selection);
	}
	
	@JmsListener(destination = "selection.queue.second")
	void secondSelection(Message message) throws JMSException {
		/*Destination destination = message.getJMSReplyTo();*/
		ObjectMessage objMessage = (ObjectMessage) message;
		Selection selection = (Selection) objMessage.getObject();
		if (selection.getOperationType()) {	//选课
			List<CourseInfo> courseInfos=selection.getCourseInfos();
			List<Long> coursesId=courseInfos.stream().map(CourseInfo::getCourseId).sorted((a,b)->b.compareTo(a)).collect(Collectors.toList());
			List<StudentNum> studentNums=selectionMapper.selectNum(coursesId);
			Map<Long, Map> numMap=new HashMap();
			Map<Boolean, Integer> temp;
			Double credit=selection.getCredit();
			for(StudentNum num:studentNums){
				temp=new HashMap<Boolean,Integer>();
				numMap.put(num.getCourseId(), temp);
			}
			for(StudentNum num:studentNums){
				temp=numMap.get(num.getCourseId());
				temp.put(num.getType(), num.getSum());
			}
			/*List<Course> courses=courseService.getCoursesByIdList(coursesId);
			List<CourseInfo> primaryCourses=courseInfos.stream().filter(s->s.getStudentType()==1).collect(Collectors.toList());
			List<Long> primaryId=primaryCourses.stream().map(CourseInfo::getCourseId).sorted((a,b)->b.compareTo(a)).collect(Collectors.toList());
			List<CourseInfo> secondCourses=courseInfos.stream().filter(s->s.getStudentType()==2).collect(Collectors.toList());
			List<Long> secondId=secondCourses.stream().map(CourseInfo::getCourseId).sorted((a,b)->b.compareTo(a)).collect(Collectors.toList());
			List<CourseInfo> minorCourses=courseInfos.stream().filter(s->s.getStudentType()==0).collect(Collectors.toList());
			List<Long> minorId=minorCourses.stream().map(CourseInfo::getCourseId).sorted((a,b)->b.compareTo(a)).collect(Collectors.toList());*/
			
			List<CourseInfo> insertInfo=new ArrayList<>();
			Map<Boolean,Integer> temp2;
			for(CourseInfo courseInfo:courseInfos){
				temp2=numMap.get(courseInfo.getCourseId());
				int type=courseInfo.getStudentType();
				if(type==0||type==2){//主选+辅修
					if(temp2.get(false)<courseInfo.getPrimaryLimit()){
						if(credit>courseInfo.getCredit()){
							insertInfo.add(courseInfo);
							credit-=courseInfo.getCredit();
						}
					}
				}
				else{//非主选+辅修
					if(temp2.get(true)<courseInfo.getSecondLimit()){
						if(credit>courseInfo.getCredit()){
							insertInfo.add(courseInfo);
							credit-=courseInfo.getCredit();
						}
					}
				}
			}
			selection.setCourseInfos(insertInfo);
			selectionMapper.addCourses(selection);
		} else
			selectionMapper.removeCourses(selection);
	}
	
	@JmsListener(destination = "selection.queue.third")
	void thirdSelection(Message message) throws JMSException {
		/*Destination destination = message.getJMSReplyTo();*/
		ObjectMessage objMessage = (ObjectMessage) message;
		Selection selection = (Selection) objMessage.getObject();
		if (selection.getOperationType()) {	//选课
			List<CourseInfo> courseInfos=selection.getCourseInfos();
			List<Long> coursesId=courseInfos.stream().map(CourseInfo::getCourseId).sorted((a,b)->b.compareTo(a)).collect(Collectors.toList());
			List<StudentNum> studentNums=selectionMapper.selectNum(coursesId);
			Map<Long, Map> numMap=new HashMap();
			Map<Boolean, Integer> temp;
			Double credit=selection.getCredit();
			for(StudentNum num:studentNums){
				temp=new HashMap<Boolean,Integer>();
				numMap.put(num.getCourseId(), temp);
			}
			for(StudentNum num:studentNums){
				temp=numMap.get(num.getCourseId());
				temp.put(num.getType(), num.getSum());
			}
			
			List<CourseInfo> insertInfo=new ArrayList<>();
			Map<Boolean,Integer> temp2;
			for(CourseInfo courseInfo:courseInfos){
				temp2=numMap.get(courseInfo.getCourseId());
				if(temp2.get(false)<courseInfo.getPrimaryLimit()){//主选有空位加入主选
					if(credit>courseInfo.getCredit()){
						insertInfo.add(courseInfo);
						credit-=courseInfo.getCredit();
					}
				}
				if(temp2.get(true)<courseInfo.getSecondLimit()){//主选没有空位加入非主选
					if(credit>courseInfo.getCredit()){
						insertInfo.add(courseInfo);
						credit-=courseInfo.getCredit();
					}
				}
			}
			selection.setCourseInfos(insertInfo);
			selectionMapper.addCourses(selection);
		} else
			selectionMapper.removeCourses(selection);
	}
	
	
	
}
